﻿/* Copyright 2010 Igor Francé
 * 
 * This file is part of ECMADoc (http://ecmadoc.codeplex.com).
 * 
 * ECMADoc is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * ECMADoc is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with ECMADoc. 
 * If not, see http://www.gnu.org/licenses/.
 */
namespace EcmaDoc.Core.Configuration
{
	using System;
	using System.Text.RegularExpressions;
	using System.Xml;

	/// <summary>
	/// Defines an <c>EcmaDoc</c> match expression.
	/// </summary>
	internal class ExpressionInfo
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="ExpressionInfo"/> class. 
		/// </summary>
		/// <param name="infoNode">The XML configuration node that defines this expression.</param>
		public ExpressionInfo(XmlElement infoNode)
		{
			this.Name = infoNode.GetAttribute("name");
			this.HandlerName = infoNode.GetAttribute("handler");
			this.Description = infoNode.GetAttribute("description");
			this.ExpressionText = infoNode.SelectSingleNode("c:rxp", ParserConfig.NamespaceManager).InnerText;

			this.Expression = new Regex(this.ExpressionText.Trim(), RegexOptions.IgnorePatternWhitespace);
		}

		/// <summary>
		/// Gets the name of this expression.
		/// </summary>
		public string Name { get; private set; }

		/// <summary>
		/// Gets the name of the delegate that handles this expression.
		/// </summary>
		public string HandlerName { get; private set; }

		/// <summary>
		/// Gets the description of this expression.
		/// </summary>
		public string Description { get; private set; }

		/// <summary>
		/// Gets the text listeral of this expression.
		/// </summary>
		public string ExpressionText { get; private set; }

		/// <summary>
		/// Gets the active regular expression.
		/// </summary>
		public Regex Expression { get; private set; }

		/// <summary>
		/// Returns a <see cref="String"/> that represents this instance.
		/// </summary>
		/// <returns>
		/// A <see cref="String"/> that represents this instance.
		/// </returns>
		public override string ToString()
		{
			return this.Name + ": " + this.HandlerName;
		}

		internal XmlElement ToXml(XmlDocument ownerDoc)
		{
			XmlElement self = ownerDoc.CreateElement("expression", ParserConfig.EcmaDocNamespace);
			self.SetAttribute("name", this.Name);
			self.SetAttribute("handler", this.HandlerName);
			self.SetAttribute("description", this.Description);

			XmlNode xprNode = self.AppendChild(ownerDoc.CreateElement("rxp", ParserConfig.EcmaDocNamespace));
			xprNode.AppendChild(ownerDoc.CreateCDataSection(this.ExpressionText));

			return self;
		}
	}
}